import java.util.HashMap;
import java.util.Map;

// 5482660
public class P44 {
	private static Map<Long, Long> map = new HashMap<Long, Long>();	
	private static long min = Integer.MAX_VALUE;
	
	private static long getPentagonNumber(long n) {
		return (n * (3 * n - 1))/2;
	}
	
	private static void search(long n) {
		while (n > map.size()) {
			long x = map.size();
			if (x == 0) {
				x = 1;
			} else {
				x += 1;
			}
			
			x = getPentagonNumber(x);
			map.put(x, x);
		}
		
		//System.out.println(n);
		//System.out.println(list.size());
		
		for (long i = 0; i < n - 1; i++) {
			
			long a = map.get(getPentagonNumber(n));
			long b = map.get(getPentagonNumber(n - 1 - i));
						
			
			long diff = a - b;
			if (diff > min) {
				break;
			}
			
			if (map.containsKey(diff)) {
				
				long sum = a + b;
				
				while (true) {
					if (map.containsKey(sum)) {							
						if (min > diff) {
							min = diff;
							System.out.println(a + ", " + b);
							System.out.println("min:" + diff);
						}
						break;
					} else if (sum < map.get(getPentagonNumber(map.size()))) {
						break;
					} else {
						long x = getPentagonNumber(map.size() + 1);
						map.put(x, x);
					}						
				}							
			}
		}
	}
	
	public static void main(String[] args)  {
		try {
			long time = System.currentTimeMillis();
			long n = 2;
			while (true) {
				//System.out.println(n);
				search(n);
				
				//System.out.println(map.size() + ":" +( getPentagonNumber(n + 1) - getPentagonNumber(n)));
				if (min < getPentagonNumber(n + 1) - getPentagonNumber(n)) {
					break;
				} else {
					n++;
				}
//				
//				Thread.sleep(1000);
//				
//				for (Integer i : list) {
//					System.out.println(i);
//				}
//				System.out.println();
			}
			
			System.out.println(">>>" + min);
			System.out.println(System.currentTimeMillis() - time);	
		} catch (Throwable e) {
			e.printStackTrace();
		}
		
	}
}
